package com.txby.wxpay;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * 微信支付配置基本信息
* @ClassName: WXPayConfigImpl 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author jacking
* @date 2017年10月30日 下午5:52:28 
 */
@Data
public class WXPayConfigImpl extends WXPayConfig{
	/**
	 * 微信支付java p12证书，退款时使用
	 */
    private byte[] certData;
    /**
     * 证书绝对路径
     */
	private String certPath = "E:/cert/apiclient_cert.p12";
	private String primaryDomain = "api.mch.weixin.qq.com";
	private String alternateDomain = "api2.mch.weixin.qq.com";
	/**
	 *进行健康上报的线程的数量
	 */
	private int reportWorkerNum = 1;
	/**
	 * 批量上报，一次最多上报多个数据
	 */
	private int reportBatchSize = 2;

    /**
     * 单例对象
     */
    private static WXPayConfigImpl INSTANCE;

    private WXPayConfigImpl(){
        File file = new File(certPath);
        if(file.exists()) {
        	try {
				InputStream certStream = new FileInputStream(file);
				this.certData = new byte[(int) file.length()];
				certStream.read(this.certData);
				certStream.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
        }
        
    }
    private WXPayConfigImpl(String certPath,String app_id,String mch_id,String key){
    	this.certPath=certPath;
    	this.app_id=app_id;
    	this.mch_id=mch_id;
    	this.key=key;
    	File file = new File(certPath);
    	if(file.exists()) {
    		InputStream certStream;
			try {
				certStream = new FileInputStream(file);
				this.certData = new byte[(int) file.length()];
				certStream.read(this.certData);
				certStream.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	} 
    }
    private WXPayConfigImpl(String certPath,String app_id,String mch_id,String key,int reportWorkerNum,int reportBatchSize){
    	this.certPath=certPath;
    	this.app_id=app_id;
    	this.mch_id=mch_id;
    	this.key=key;
    	this.reportWorkerNum=reportWorkerNum;
    	this.reportBatchSize=reportBatchSize;
    	File file = new File(certPath);
    	if(file.exists()) {
    		try {
				InputStream certStream = new FileInputStream(file);
				this.certData = new byte[(int) file.length()];
				certStream.read(this.certData);
				certStream.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	} 
    }
	/**
	 * 获取单例对象
	* @Title: getInstance 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param @return
	* @param @throws Exception    设定文件 
	* @return WXPayConfigImpl    返回类型 
	* @throws
	 */
    public static WXPayConfigImpl getInstance(){
        if (INSTANCE == null) {
            synchronized (WXPayConfigImpl.class) {
                if (INSTANCE == null) {
                    INSTANCE = new WXPayConfigImpl();
                }
            }
        }
        return INSTANCE;
    }
    /**
     *  获取非单例对象
    * @Title: getInstance 
    * @Description: TODO(这里用一句话描述这个方法的作用) 
    * @param @param newObject
    * @param @return
    * @param @throws Exception    设定文件 
    * @return WXPayConfigImpl    返回类型 
    * @throws
     */
    public static WXPayConfigImpl getInstanceNewObject(String certPath,String app_id,String mch_id,String key){
    	return	  new WXPayConfigImpl(certPath, app_id, mch_id, key);
    }

    
     
    public String getAppID() {
        return this.app_id;
    }

    public String getMchID() {
        return this.mch_id;
    }

    public String getKey() {
        return this.key;
    }

    public InputStream getCertStream() {
        ByteArrayInputStream certBis;
        certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }


    public int getHttpConnectTimeoutMs() {
        return 2000;
    }

    public int getHttpReadTimeoutMs() {
        return 10000;
    }

    IWXPayDomain getWXPayDomain() {
        return WXPayDomainSimpleImpl.instance();
    }

    public String getPrimaryDomain() {
        return primaryDomain;
    }

    public String getAlternateDomain() {
        return alternateDomain;
    }

    @Override
    public int getReportWorkerNum() {
        return reportWorkerNum;
    }

    @Override
    public int getReportBatchSize() {
        return reportBatchSize;
    }
}
